package cn.kwq.project_manger.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.kwq.project_manger.aop.annotations.Idempotency;
import cn.kwq.project_manger.common.R;
import cn.kwq.project_manger.common.RES;
import cn.kwq.project_manger.exception.SaveException;
import cn.kwq.project_manger.pojo.dto.*;
import cn.kwq.project_manger.service.ProjectService;
import cn.kwq.project_manger.utils.IPUtils;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Slf4j
@Api(tags ="项目服务")
@RestController
@RequestMapping("/project/")
public class ProjectController {
    @Autowired
    ProjectService projectService;

    /**
     * @return
     */
    @ApiOperation("获取比赛（只显示已通过审核的）")
    @SaIgnore//可以匿名访问该接口
    @PostMapping("getGame")
    public RES getGame(@RequestBody FindGameDto findGameDto) {
        return R.ok(projectService.findGame(findGameDto));
    }

    /**
     * @return
     */
    @ApiOperation("获取项目（只显示已通过审核的）")
    @SaIgnore
    @PostMapping("getProjects")
    public RES getProject(@RequestBody FindProjectDto dto) {

        return R.ok(projectService.findProject(dto));
    }


    /**
     * @return
     */
    @ApiOperation("发布和修改项目")
    @SaCheckLogin
    @PostMapping("addProject")
    public RES addProject(@RequestBody AddProJDto project, HttpServletRequest request) throws SaveException {
        try {
            return R.ok("发布成功，等待审核",projectService.addProject(project,StpUtil.getLoginIdAsLong()));
        }catch (SaveException e){
            String ipAddr = IPUtils.getIpAddr(request);
            log.warn("非法用户真实访问ip为"+"["+ipAddr+"]");
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * @return
     */
    @ApiOperation("添加到收藏")
    @SaCheckLogin
    @Idempotency//保证幂等性
    @PostMapping("addLike")
    public RES addLike(@ApiParam(name = "projectId", value = "项目的id", required = true)
            @RequestBody HashMap<Object, Long> map) throws Exception {
        Long projectId = map.get("projectId");
        ResProJDto resProJDto = projectService.addProjectLike(projectId, StpUtil.getLoginIdAsLong());
        return R.ok("收藏成功", resProJDto);
    }


    /**
     * @return
     */
    @ApiOperation("找到本人全部收藏的项目（未分页）")
    @SaCheckLogin
    @PostMapping ("findAllLike")
    public RES findAllLike(@ApiParam(name = "type", value = "项目的类型1比赛0项目(不筛选就是null，不传可能出错)", required = true)
                               @RequestBody JSONObject object) {
        Integer type = (Integer) object.get("type");
        return R.ok(projectService.findAllLikeProJ(StpUtil.getLoginIdAsLong(),type));
    }


    /**
     * @return
     */
    @ApiOperation("找到本人全部创建的项目/比赛（未分页）")
    @SaCheckLogin
    @GetMapping ("findAllMake")
    public RES findAllMake() {
        return R.ok(projectService.findAllMakeProJ(StpUtil.getLoginIdAsLong()));
    }


    @ApiOperation("根据id查询比赛信息（url传projectId）")
    @SaCheckLogin
    @GetMapping ("findProjOrGameById/{projectId}")
    public RES findProjById(@PathVariable("projectId") Long projectId ) {
        return R.ok(projectService.findProjectById(projectId));
    }











    }
